﻿namespace ExamV5
{
    using System;
    using System.IO;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;

    public class FrmDownLoad : Page
    {
        protected HtmlForm form1;

        protected void Page_Load(object sender, EventArgs e)
        {
            string s = (base.Request.QueryString["FileName"] != null) ? base.Request.QueryString["FileName"] : "";
            s = base.Server.MapPath(".") + @"\PublicFile\" + s;
            s = base.Server.UrlDecode(s);
            if (!File.Exists(s))
            {
                base.Response.Write("<script langauge=javascript>alert('文件不存在！');history.go(-1);</script>");
                base.Response.End();
            }
            else
            {
                FileInfo info = new FileInfo(s);
                base.Response.Clear();
                base.Response.ClearHeaders();
                base.Response.Buffer = false;
                base.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName(s), Encoding.Default));
                base.Response.AppendHeader("Content-Length", info.Length.ToString());
                base.Response.ContentType = "application/octet-stream";
                base.Response.WriteFile(s);
                base.Response.Flush();
                base.Response.End();
                base.Response.Write("<script>window.close();</script>");
            }
        }
    }
}

